// 统一导航栏功能
class Navbar {
    constructor() {
        this.currentUser = null;
        this.userType = null;
        this.init();
    }

    init() {
        this.checkLoginStatus();
        this.setupEventListeners();
        this.updateActiveNavItem();
    }

    checkLoginStatus() {
        const userInfo = localStorage.getItem('userInfo');
        const adminInfo = localStorage.getItem('adminInfo');
        const userType = localStorage.getItem('userType');

        if (userInfo && userType === 'user') {
            this.currentUser = JSON.parse(userInfo);
            this.userType = 'user';
        } else if (adminInfo && userType === 'admin') {
            this.currentUser = JSON.parse(adminInfo);
            this.userType = 'admin';
        } else {
            // 未登录，跳转到登录页面
            if (window.location.pathname !== '/login.html' && 
                window.location.pathname !== '/index.html' &&
                window.location.pathname !== '/register.html') {
                window.location.href = 'login.html';
            }
            return;
        }

        this.updateUserInfo();
    }

    updateUserInfo() {
        const userNameElement = document.getElementById('userName');
        const userAvatarElement = document.getElementById('userAvatar');
        
        if (userNameElement && this.currentUser) {
            const displayName = this.userType === 'admin' ? 
                this.currentUser.adminName : this.currentUser.username;
            userNameElement.textContent = displayName;
        }

        if (userAvatarElement && this.currentUser) {
            const displayName = this.userType === 'admin' ? 
                this.currentUser.adminName : this.currentUser.username;
            userAvatarElement.textContent = displayName.charAt(0).toUpperCase();
        }
    }

    setupEventListeners() {
        // 退出登录按钮
        const logoutBtn = document.getElementById('logoutBtn');
        if (logoutBtn) {
            logoutBtn.addEventListener('click', () => this.logout());
        }

        // 导航链接点击事件
        const navLinks = document.querySelectorAll('.nav-link');
        navLinks.forEach(link => {
            link.addEventListener('click', (e) => {
                // 移除所有active类
                navLinks.forEach(l => l.classList.remove('active'));
                // 添加active类到当前点击的链接
                e.currentTarget.classList.add('active');
            });
        });
    }

    updateActiveNavItem() {
        const currentPath = window.location.pathname;
        const navLinks = document.querySelectorAll('.nav-link');
        
        navLinks.forEach(link => {
            const href = link.getAttribute('href');
            if (href && currentPath.includes(href.replace('.html', ''))) {
                link.classList.add('active');
            }
        });
    }

    logout() {
        // 清除所有存储的信息
        localStorage.removeItem('userInfo');
        localStorage.removeItem('adminInfo');
        localStorage.removeItem('userType');
        localStorage.removeItem('selectedCategory');
        localStorage.removeItem('openAddModal');
        
        // 跳转到登录页面
        window.location.href = 'login.html';
    }

    // 获取当前用户信息
    getCurrentUser() {
        return this.currentUser;
    }

    // 获取用户类型
    getUserType() {
        return this.userType;
    }

    // 检查是否是管理员
    isAdmin() {
        return this.userType === 'admin';
    }

    // 检查是否是普通用户
    isUser() {
        return this.userType === 'user';
    }
}

// 创建全局导航栏实例
window.navbar = new Navbar();

// 导出供其他脚本使用
if (typeof module !== 'undefined' && module.exports) {
    module.exports = Navbar;
} 